package cn.fl.settlement.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.service.ICapFundService;
import cn.fl.capital.vo.CapFundVO;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.constant.InsuranceConstant;
import cn.fl.project.condition.PrjPrdDtlQueryCondition;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjPrdDtlService;
import cn.fl.project.service.IPrjPrdPaySchMService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdDtlVO;
import cn.fl.project.vo.PrjPrdPaySchMVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @Description:    结清来款资金管理Controller
* @Author:         liurong
* @CreateDate:     2020/12/24 15:04
* @Version:        1.0
*/
@Controller
@RequestMapping("/settlement/fund")
public class SettleFundController extends BaseController {

    @Autowired
    private IPrjPrdDtlService prjPrdDtlService;

    @Autowired
    private IPrjPrdPaySchMService prjPrdPaySchMService;

    @Autowired
    private IPrjBscInfoService prjBscInfoService;

    @Autowired
    private ICapFundService capFundService;

    @Autowired
    private ICstBscInfoService cstBscInfoService;

    @Autowired
    private ISysDictionaryApiService dictionaryApiService;

    /**
     * 到结清来款资金维护页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toBindFundPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toRefundListPage(HttpServletRequest request) throws BaseException {
        ModelAndView view = new ModelAndView("settlement/common/settleBindFund");
        String paySchId = request.getParameter("paySchId");
        String wsPaySchId = request.getParameter("wsPaySchId");
        if (StringUtils.isEmpty(paySchId)) {
            throw new BaseException("支付表ID不能为空！");
        }
        if (StringUtils.isEmpty(wsPaySchId)) {
            throw new BaseException("我司支付表ID不能为空！");
        }
        // 查询支付表信息
        PrjPrdPaySchMVO paySchMVO = prjPrdPaySchMService.selectById(Long.parseLong(paySchId));
        if (paySchMVO == null) {
            throw new BaseException("支付表信息不存在！");
        }
        //查询项目信息
        PrjBscInfoVO prjBscInfoVO = prjBscInfoService.selectById(paySchMVO.getPrjId());
        if (prjBscInfoVO == null) {
            throw new BaseException("项目信息不存在！");
        }
        // 查询结清时间
        PrjPrdDtlQueryCondition dtlQueryCondition = new PrjPrdDtlQueryCondition();
        dtlQueryCondition.setPaySchId(Long.parseLong(wsPaySchId));// 我司支付表ID
        List<PrjPrdDtlVO> prdDtlVOList = this.prjPrdDtlService.selectList(dtlQueryCondition);
        if (CollectionUtils.isNotEmpty(prdDtlVOList)) {
            PrjPrdDtlVO prjPrdDtlVO = prdDtlVOList.get(0);
            if (null != prjPrdDtlVO.getStlDt()) {
                view.addObject("stlDt", DateUtils.formatDateToString(prjPrdDtlVO.getStlDt(), DateUtils.YYYY_MM_DD));
            }
            view.addObject("prjPrdDtlId", prjPrdDtlVO.getId());
        }

        CapFundVO fundVO = new CapFundVO();//来款信息
        fundVO.setRelationPayId(Long.parseLong(paySchId));//资金管理关联业务ID
        fundVO.setIdentifyOperator(CurrentThreadContext.getCurrentUserId());//认款人ID
        fundVO.setIdentifyOperatorNm(CurrentThreadContext.getCurrentUserName());//认款人名称
        fundVO.setIdentifyTime(new Date());//认款时间 add时会修改
        //默认财务收款账户、账号
        fundVO.setAcceptName(InsuranceConstant.CODE_FC_ACCEPT_ACCOUNT_NM);//收款账户
        fundVO.setAcceptCode(InsuranceConstant.CODE_FC_ACCEPT_ACCOUNT_CD);//收款账号
        fundVO.setPaySchNo(paySchMVO.getPaySchNo());

        //查询客户名称，客户ID,所属事业部
        fundVO.setCstId(paySchMVO.getCstId());
        fundVO.setCstNm(prjBscInfoVO.getCstNm());
        fundVO.setFundBelongOrgCd(prjBscInfoVO.getBuOrgCd());
        fundVO.setComeName(prjBscInfoVO.getCstNm());

        //交易日期与实际到账日期默认流程发起时间
        Date flowStartTm = capFundService.getFlowStartTime(Long.parseLong(paySchId), "3");
        if (null != flowStartTm) {
            fundVO.setAcceptDate(DateUtils.formatDate(flowStartTm, DateUtils.YYYY_MM_DD));
            fundVO.setAcceptActualDate(DateUtils.formatDate(flowStartTm, DateUtils.YYYY_MM_DD));
            fundVO.setAcceptDates(DateUtils.formatDateToString(fundVO.getAcceptDate(), DateUtils.YYYY_MM_DD));
            fundVO.setAcceptActualDates(DateUtils.formatDateToString(fundVO.getAcceptActualDate(), DateUtils.YYYY_MM_DD));
        }

        // 事业部
        List<KeyValueVO> orgList = cstBscInfoService.searchBusinessOrgList();
        view.addObject("fundBelongOrgCdList", JSON.toJSONString(orgList));

        view.addObject("cstId", fundVO.getCstId());
        view.addObject("paySchId", paySchId);
        view.addObject("wsPaySchId", wsPaySchId);
        view.addObject("capFundVO", fundVO);
        return view;
    }




}

